import datetime
import subprocess
from time import mktime


def sub_time(minutes):
    now = datetime.datetime.now()
    delta = datetime.timedelta(minutes=minutes)
    hour_ago = mktime((now - delta).timetuple())
    return hour_ago


def test_ping(host):
    cmd = 'ping -c %d %s' % (2, host)  # 获取ping测试主机地址
    p = subprocess.Popen(args=cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)  # 执行ping命令，并输出命令执行结果
    (stdout, errout) = p.communicate()  # 保存输出结果
    output = stdout.decode()  # 因为communicate()输出的结果为bytes类型，转换为字符串
    if output.find("ttl=") >= 0:  # 对输出信息进行筛选，输出结果
        return True
    return False
